package com.ai.smlcore.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;


public class TenpayUtil {


    public static Set<Integer> randomSet = new HashSet<Integer>();
    public static String randomDate ="";

    /**
     * 把对象转换成字符串
     * @param obj
     * @return String 转换成字符串,若对象为null,则返回空字符串.
     */
    public static String toString(Object obj) {
        if(obj == null) {
            return "";
        }

        return obj.toString();
    }

    /**
     * 把对象转换为int数值.
     *
     * @param obj
     *            包含数字的对象.
     * @return int 转换后的数值,对不能转换的对象返回0。
     */
    public static int toInt(Object obj) {
        int a = 0;
        try {
            if (obj != null) {
                a = Integer.parseInt(obj.toString());
            }
        } catch (Exception e) {

        }
        return a;
    }

    /**
     * 获取当前时间 yyyyMMddHHmmss
     * @return String
     */
    public static String getCurrTime() {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = outFormat.format(now);
        return s;
    }

    /**
     * 获取当前时间 yyyyMMddHHmmss
     * @return String
     */
    public static String getCurrTime(String format) {
        Date now = new Date();
        SimpleDateFormat outFormat = new SimpleDateFormat(format);
        String s = outFormat.format(now);
        return s;
    }
    /**
     * 获取当前日期 yyyyMMdd
     * @param date
     * @return String
     */
    public static String formatDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String strDate = formatter.format(date);
        return strDate;
    }

    /**
     * 取出一个指定长度大小的随机正整数.
     *
     * @param length
     *            int 设定所取出随机数的长度。length小于11
     * @return int 返回生成的随机数。
     */
    public static int buildRandom(int length) {
        int num = 1;
        double random = Math.random();
        if (random < 0.1) {
            random = random + 0.1;
        }
        for (int i = 0; i < length; i++) {
            num = num * 10;
        }
        return (int) (random * num);
    }

    /**
     * 取出一个指定长度大小的随机正整数.
     *
     * @param length
     *            int 设定所取出随机数的长度。length小于11
     * @return int 返回生成的随机数。
     */
    public static int singleRandom(int length) {
        if (StringUtils.isBlank(TenpayUtil.randomDate)) {
            TenpayUtil.randomDate = TenpayUtil.getCurrTime("yyyyMMdd");
        }
        if (!StringUtils.equals(TenpayUtil.randomDate, TenpayUtil.getCurrTime("yyyyMMdd"))) {
            TenpayUtil.randomDate = TenpayUtil.getCurrTime("yyyyMMdd");
            TenpayUtil.randomSet.clear();
        }
        int random= TenpayUtil.buildRandom(8);
        while (!TenpayUtil.randomSet.add(random)) {
            ;
        }
        return random;
    }

    /**
     * 获取编码字符集
     * @param request
     * @param response
     * @return String
     */
    public static String getCharacterEncoding(HttpServletRequest request,
        HttpServletResponse response) {

        if(null == request || null == response) {
            return "gbk";
        }

        String enc = request.getCharacterEncoding();

        if(null == enc || "".equals(enc)) {
            enc = response.getCharacterEncoding();
        }

        if(null == enc || "".equals(enc)) {
            enc = "gbk";
        }

        return enc;
    }

    /**
     * 获取unix时间，从1970-01-01 00:00:00开始的秒数
     * @param date
     * @return long
     */
    public static long getUnixTime(Date date) {
        if( null == date ) {
            return 0;
        }

        return date.getTime()/1000;
    }

    /**
     * 时间转换成字符串
     * @param date 时间
     * @param formatType 格式化类型
     * @return String
     */
    public static String date2String(Date date, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        return sdf.format(date);
    }
    public static String getFormattedDate(String strDate, String strFormatTo) {
        try {
            if (strDate == null || strDate.trim().equals("")) {
                return "";
            }
            strDate = strDate.replace('/', '-');
            strFormatTo = strFormatTo.replace('/', '-');
            if (strDate.equals("0000-00-00 00:00:00")
                    || strDate.equals("1800-01-01 00:00:00")) {
                return "";
            }
            String formatStr = strFormatTo; //"yyyyMMdd";
            if (strDate.trim().equals("")) {
                return "";
            }
            switch (strDate.trim().length()) {
            case 6:
                if (strDate.substring(0, 1).equals("0")) {
                    formatStr = "yyMMdd";
                } else {
                    formatStr = "yyyyMM";
                }
                break;
            case 8:
                formatStr = "yyyyMMdd";
                break;
            case 10:
                if (strDate.indexOf("-") == -1) {
                    formatStr = "yyyy/MM/dd";
                } else {
                    formatStr = "yyyy-MM-dd";
                }
                break;
            case 11:
                if (strDate.getBytes().length == 14) {
                    formatStr = "yyyy年MM月dd日";
                } else {
                    return "";
                }
            case 14:
                formatStr = "yyyyMMddHHmmss";
                break;
            case 19:
                if (strDate.indexOf("-") == -1) {
                    formatStr = "yyyy/MM/dd HH:mm:ss";
                } else {
                    formatStr = "yyyy-MM-dd HH:mm:ss";
                }
                break;
            case 21:
                if (strDate.indexOf("-") == -1) {
                    formatStr = "yyyy/MM/dd HH:mm:ss.S";
                } else {
                    formatStr = "yyyy-MM-dd HH:mm:ss.S";
                }
                break;
            default:
                return strDate.trim();
            }

            SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(formatter.parse(strDate));
            formatter = new SimpleDateFormat(strFormatTo);
            return formatter.format(calendar.getTime());
        } catch (Exception e) {
            //Common.printLog("转换日期字符串格式时出错;" + e.getMessage());
            return "";
        }
    }
    /**
     * int subtractYear
     * @return String
     */
    public static String setCurrTimeSubtractYear(int subtractYear,int subtractDate,String formatType) {
        java.util.Calendar ca = java.util.Calendar.getInstance();
        ca.add(Calendar.YEAR, subtractYear);
        ca.add(Calendar.DATE, subtractDate);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(formatType);
        String result = sdf.format(ca.getTime());
        return result;
    }
    /**
     * int subtractYear
     * @return String
     */
    public static String setCurrTimeSubtractMonth(int subtractMonth,int subtractDate,String formatType) {
        java.util.Calendar ca = java.util.Calendar.getInstance();
        ca.add(Calendar.MONTH, subtractMonth);
        ca.add(Calendar.DATE, subtractDate);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(formatType);
        String result = sdf.format(ca.getTime());
        return result;
    }

    /**
     * int subtractYear
     * @return String
     */
    public static String setCurrTimeSubtractMonth(Date date,
        int subtractMonth,int subtractDate,String formatType) {
        java.util.Calendar ca = java.util.Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.MONTH, subtractMonth);
        if(subtractDate!=0){
            ca.add(Calendar.DATE, subtractDate);
        }

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(formatType);
        String result = sdf.format(ca.getTime());
        return result;
    }
    /**
     * 取得日期的前一天 yyyy-MM-dd
     * @return String
     */
    public static String yesterday() {
        java.util.Calendar ca = java.util.Calendar.getInstance();
        ca.add(Calendar.DAY_OF_MONTH, -1);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(ca.getTime());
        return result;
    }
    /**
     * 取得月的最后一天 yyyyMMdd
     * @return String
     */
    public static String getMonthLast() {
        java.util.Calendar ca = java.util.Calendar.getInstance();
        ca.setTime(new Date());
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(ca.getTime());
        return result;
    }
    /**
     * 取得月的第一天 yyyyMMdd
     * @return String
     */
    public static String getMonthFirst() {
        java.util.Calendar ca = java.util.Calendar.getInstance();
        ca.setTime(new Date());
        ca.set(Calendar.DATE, ca.getActualMinimum(Calendar.DATE));
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(ca.getTime());
        return result;
    }

    public static void main(String[] args) {
        System.out.println(TenpayUtil.getMonthFirst());
    }

}
